package com.weigee.ktv.v.adapter.rv;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.l.d.app.library.adapter.RvListAdapter;
import com.l.d.app.library.view.ShoppingCartIncreaseOrDecreaseView;
import com.weigee.ktv.R;
import com.weigee.ktv.m.entity.SupermarketChooseGoodsRightEntity;
import com.weigee.ktv.m.retrofit.ApiService;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import skin.support.widget.SkinCompatImageView;
import skin.support.widget.SkinCompatTextView;

/**
 * Created by linde on 2017/4/29.
 * <p/>
 */
public class SupermarketChooseGoodsRightAdapter extends RvListAdapter<SupermarketChooseGoodsRightEntity, SupermarketChooseGoodsRightAdapter.Holder> {

    @NonNull
    @Override
    public List<SupermarketChooseGoodsRightEntity> onCreateNewEmptyList() {
        return new ArrayList<>();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        final Context context = parent.getContext();
        final View itemView = LayoutInflater.from(context).inflate(R.layout.supermarket_frag_choose_goods___rv_item_right, parent, false);
        return new Holder(itemView);
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        final SupermarketChooseGoodsRightEntity entity = getItem(position);
        if (entity == null) {
            return;
        }
        final Context context = holder.itemView.getContext();

        Glide.with(context).load(ApiService.BASE_IMG_URL + entity.getImg()).asBitmap().into(holder.ivPurchaseLogo);
        holder.tvTitle.setText(entity.getName());
        holder.tvBrief.setText(entity.getDescription());
        holder.tvPrice.setText(String.valueOf("¥" + entity.getPrice()));

        final int cartNumber = entity.getCartNumber();
        holder.tvPurchaseNumber.setText(String.valueOf(cartNumber));
        if (cartNumber > 0) {
            holder.tvPurchaseNumber.setVisibility(View.VISIBLE);
            holder.tvPurchaseDecrease.setVisibility(View.VISIBLE);
        } else {
            holder.tvPurchaseNumber.setVisibility(View.GONE);
            holder.tvPurchaseDecrease.setVisibility(View.GONE);
        }

        // 加入购物车（+1）
        holder.tvPurchaseIncrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                entity.setCartNumber(cartNumber < 0 ? 1 : cartNumber + 1);
                notifyDataSetChanged();
            }
        });
        // 从购物车移出（-1）
        holder.tvPurchaseDecrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                entity.setCartNumber(cartNumber < 1 ? 0 : cartNumber - 1);
                notifyDataSetChanged();
            }
        });
    }

    static class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_purchase_logo)
        SkinCompatImageView ivPurchaseLogo;
        @BindView(R.id.tv_title)
        SkinCompatTextView tvTitle;
        @BindView(R.id.tv_brief)
        SkinCompatTextView tvBrief;
        @BindView(R.id.tv_price)
        SkinCompatTextView tvPrice;
        @BindView(R.id.tv_purchase_increase)
        ShoppingCartIncreaseOrDecreaseView tvPurchaseIncrease;
        @BindView(R.id.tv_purchase_number)
        SkinCompatTextView tvPurchaseNumber;
        @BindView(R.id.tv_purchase_decrease)
        ShoppingCartIncreaseOrDecreaseView tvPurchaseDecrease;

        Holder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
